/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package piedrapapeltijerasw;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.FillRule;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.util.Duration;

/**
 * FXML Controller class
 *
 * @author ias
 */
public class GameScreenController implements Initializable {

    @FXML
    private Pane panelPrueba;

    @FXML
    private Label cronometroRound;

    public ImageView image1, image2, image3;

    private Integer minutos = 0 , segundos = 0, milesimas = 0;
    
    private String min="", seg="";
    
    private AnimationTimer timer;
    
    private PathTransition pathTransition1, pathTransition2, pathTransition3;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

        image1 = (ImageView) panelPrueba.lookup("#descarga");
        image2 = (ImageView) panelPrueba.lookup("#descarga1");
        image3 = (ImageView) panelPrueba.lookup("#descarga2");

        Path path1 = createEllipsePath(90, 100, 130, 50, Color.TRANSPARENT, Color.TRANSPARENT);
        Path path2 = createEllipsePath(180, 70, 130, 50, Color.TRANSPARENT, Color.TRANSPARENT);
        Path path3 = createEllipsePath(0, 80, 80, 50, Color.TRANSPARENT, Color.TRANSPARENT);

        add(panelPrueba, path1);
        add(panelPrueba, path2);
        add(panelPrueba, path3);

        pathTransition1 = createPathTransition(200, path1, image1, Timeline.INDEFINITE, true);
        pathTransition1.play();

        pathTransition2 = createPathTransition(200, path2, image2, Timeline.INDEFINITE, true);
        pathTransition2.play();

        pathTransition3 = createPathTransition(200, path3, image3, Timeline.INDEFINITE, true);
        pathTransition3.play();

        iniciaCronometro();

    }

    private void add(Pane pane, Node node) {
        pane.getChildren().add(node);
    }

    private PathTransition createPathTransition(double durationMillis, Path Path, Node node, int cycleCount, boolean autoReverse) {
        PathTransition pathTransition = new PathTransition();
        pathTransition.setDuration(Duration.millis(durationMillis));
        pathTransition.setPath(Path);
        pathTransition.setNode(node);
        pathTransition.setCycleCount(cycleCount);
        pathTransition.setAutoReverse(autoReverse);
        return pathTransition;
    }

    private Path createEllipsePath(double rotation, double centerX, double centerY, double innerRadius, Color bgColor, Color strkColor) {
        Path path = new Path();
        path.setFill(bgColor);
        path.setStroke(strkColor);
        path.setFillRule(FillRule.EVEN_ODD);
        path.setRotate(rotation);

        MoveTo moveTo = new MoveTo();
        moveTo.setX(centerX + innerRadius);
        moveTo.setY(centerY);

        ArcTo arcToInner = new ArcTo();
        arcToInner.setX(centerX - innerRadius);
        arcToInner.setY(centerY);
        arcToInner.setRadiusX(innerRadius);
        arcToInner.setRadiusY(innerRadius);

        path.getElements().add(moveTo);
        path.getElements().add(arcToInner);

        return path;

    }

    public void iniciaCronometro() {

        timer = new AnimationTimer() {
            @Override
            public void handle(long l) {

                segundos += 1;
                if (segundos == 60) {
                    segundos = 0;
                    minutos++;
                }
                
                if( minutos < 10 ) min = "0" + minutos;
                else min = minutos.toString();
                if( segundos < 10 ) seg = "0" + segundos;
                else seg = segundos.toString();
                 
                cronometroRound.setText( min + ":" + seg );
                if(min.equals("05")){
                    timer.stop();
                    pathTransition1.stop();
                    pathTransition2.stop();
                    pathTransition3.stop();
                }
            }

        };

        timer.start();
    }
}
